package com.aaa.backsystem.controller;


import com.aaa.backsystem.aop.SaveOrUpdateEntityAnn;
import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.DeptService;
import com.aaa.backsystem.util.MyConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static java.awt.SystemColor.menu;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-07-23
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    /**
     * 页面跳转至部门管理页面
     *
     * @return
     */
    @RequestMapping("/toShowDept")
    public String toShowDept() {
        return "dept/showDept";
    }

    @RequestMapping("/findAllDeptTreeTable")
    @ResponseBody
    public LayuiTable findAllDeptTreeTable() {
        List<Dept> deptList = deptService.selectList(null);

        LayuiTable table = new LayuiTable();
        table.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        table.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        table.setData(deptList);
        table.setCount(deptList.size());
        return table;
    }

    /**
     * 根据deptId删除dept
     *
     * @param deptId
     * @return
     */
    @RequestMapping("/deleteDept")
    @ResponseBody
    public Result deleteMenu(Integer deptId) {
        //删除dept表
        boolean flag = deptService.deleteById(deptId);

        if (flag) {
            return super.success();
        } else {
            return super.error();
        }
    }

    /**
     * 根据父ID找到部门对应父级名字
     *
     * @param deptId
     * @return
     */
    @RequestMapping("/findParentNameId")
    @ResponseBody
    public Result findParentNameId(Integer deptId) {
        Dept dept = deptService.selectById(deptId);
        if (null != dept) {
            return success(dept);
        } else {
            //menuId为0的时候，返回的对象是空，防止返回前台为空
            Dept deptName = new Dept();
            return success(deptName);
        }
    }

    /**
     * 更新部门列表
     *
     * @param dept
     * @return
     */
    @RequestMapping("/updateDept")
    @ResponseBody
    @SaveOrUpdateEntityAnn(entityClass = Dept.class)
    public Result updateDept(Dept dept) {
        boolean flag = deptService.updateById(dept);
        if (flag) {
            return success();
        }
        return error();
    }

    /**
     * 获取所有部门列表树
     * @return
     */
    @RequestMapping("/findAllDept")
    @ResponseBody
    public List<LayUiTree> findAllDept() {
        List<LayUiTree> layUiTreeList = deptService.findDepts();
        return layUiTreeList;
    }

    /**
     * 保存部门列表
     *
     * @param dept
     * @return
     */
    @RequestMapping("/saveDept")
    @ResponseBody
    @SaveOrUpdateEntityAnn(entityClass = Dept.class)
    public Result saveDept(Dept dept) {
        boolean flag = deptService.insert(dept);
        if (flag) {
            return success();
        }
        return error();
    }
}

